/**
 * UI渲染模块
 */
(function(window) {
    'use strict';
    
    /**
     * 渲染购物车
     */
    function renderCart() {
        // 确保翻译加载器存在
        if (typeof window.onTranslationsLoaded !== 'function') {
            // 如果翻译加载器不存在，直接执行渲染逻辑
            console.warn('onTranslationsLoaded 函数不存在，使用默认翻译');
            renderCartInternal();
            return;
        }
        
        window.onTranslationsLoaded(() => {
            renderCartInternal();
        });
    }
    
    /**
     * 内部渲染购物车函数
     */
    function renderCartInternal() {
            const currentLang = window.currentLang || 'zh';
            const trans = window.translations?.[currentLang];
            const cart = window.CartModule ? window.CartModule.getCart() : window.cart || [];
            const $tbody = $('#productList tbody');
            $tbody.empty();
            
            // 根据语言设置表格方向
            if (currentLang === 'uy') {
                $('#productList table').addClass('table-rtl');
            } else {
                $('#productList table').removeClass('table-rtl');
            }
            
            // 遍历购物车中的所有商品
            cart.forEach(item => {
                let row;
                
                if (currentLang === 'uy') {
                    row = `
                        <tr data-id="${item.barcode}">
                            <td>
                                <button class="delete-btn btn btn-danger btn-sm" data-id="${item.barcode}">${trans?.delete || '删除'}</button>
                            </td>
                            <td>${(item.price || 0).toFixed(2)}</td>
                            <td><input type="number" class="quantity form-control form-control-sm" value="${item.quantity || 1}" min="0"></td>
                            <td>${item.unit || '个'}</td>
                            <td><input type="number" class="price form-control form-control-sm" value="${(item.unitPrice || 0).toFixed(2)}" min="0.01" step="0.01"></td>
                            <td>${item.name || '未知商品'}</td>
                        </tr>
                    `;
                } else {
                    row = `
                        <tr data-id="${item.barcode}">
                            <td>${item.name || '未知商品'}</td>
                            <td><input type="number" class="price form-control form-control-sm" value="${(item.unitPrice || 0).toFixed(2)}" min="0.01" step="0.01"></td>
                            <td>${item.unit || '个'}</td>
                            <td><input type="number" class="quantity form-control form-control-sm" value="${item.quantity || 1}" min="0"></td>
                            <td>${(item.price || 0).toFixed(2)}</td>
                            <td>
                                <button class="delete-btn btn btn-danger btn-sm" data-id="${item.barcode}">${trans?.delete || '删除'}</button>
                            </td>
                        </tr>
                    `;
                }
                
                $tbody.append(row);
            });
            
        // 重新初始化事件监听器
        if (window.reinitEventListeners) {
            window.reinitEventListeners();
        }
    }
    
    /**
     * 渲染库存警告页面
     */
    function renderWarningPage(warningProducts, currentPage, pageSize) {
        // 确保翻译加载器存在
        if (typeof window.onTranslationsLoaded !== 'function') {
            // 如果翻译加载器不存在，直接执行渲染逻辑
            console.warn('onTranslationsLoaded 函数不存在，使用默认翻译');
            renderWarningPageInternal(warningProducts, currentPage, pageSize);
            return;
        }
        
        window.onTranslationsLoaded(() => {
            renderWarningPageInternal(warningProducts, currentPage, pageSize);
        });
    }
    
    /**
     * 内部渲染库存警告页面函数
     */
    function renderWarningPageInternal(warningProducts, currentPage, pageSize) {
            const currentLang = window.currentLang || 'zh';
            const trans = window.translations?.[currentLang];
            const start = (currentPage - 1) * pageSize;
            const end = start + pageSize;
            const currentProducts = warningProducts.slice(start, end);
            
            $('#warningTableBody').empty();
            
            if (currentProducts.length === 0) {
                $('#warningTableBody').append(`<tr><td colspan="4" class="text-center">${trans?.noWarning || '暂无库存警告'}</td></tr>`);
            } else {
                currentProducts.forEach(p => {
                    const stockClass = parseInt(p.stock) < 5 ? 'text-danger font-weight-bold' : '';
                    
                    let row;
                    if (currentLang === 'uy') {
                        row = `
                            <tr>
                                <td class="${stockClass}">${p.stock}</td>
                                <td>${p.unit}</td>
                                <td>${parseFloat(p.price || 0).toFixed(2)}</td>
                                <td>${p.name}</td>
                            </tr>
                        `;
                    } else {
                        row = `
                            <tr>
                                <td>${p.name}</td>
                                <td>${parseFloat(p.price || 0).toFixed(2)}</td>
                                <td>${p.unit}</td>
                                <td class="${stockClass}">${p.stock}</td>
                            </tr>
                        `;
                    }
                    
                    $('#warningTableBody').append(row);
                });
            }
    }
    
    // 暴露API
    window.UIModule = {
        renderCart: renderCart,
        renderWarningPage: renderWarningPage
    };
    
    // 兼容旧代码
    window.renderCart = renderCart;
    
})(window);

